import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/a_row/a_row.dart';
import 'package:im_flutter_sdk_example/pages/chat_page.dart';
import 'package:im_flutter_sdk_example/pages/items/conversation_list_item.dart';
import 'package:im_flutter_sdk_example/provider/message_model.dart';
import 'package:im_flutter_sdk_example/utils/custom_appbar.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import 'package:provider/provider.dart';

import 'widgets/conversation_item.dart';
import 'widgets/message_item.dart';

class Message extends StatefulWidget {
  static _MessageState _messageState;

  getAppBar() => _messageState.createAppBar();

  Message() {
    _messageState = _MessageState();
  }

  _MessageState createState() => _MessageState();
}

class _MessageState extends State<Message>
    implements EMConversationListItemDelegate {
  List<EMConversation> _allConversations = [];
  AppBar createAppBar() {
    return null;
  }

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      _loadConversationList();
    });
  }

  _loadConversationList() async {
    EasyMobProvider easyMobProvider = Provider.of<EasyMobProvider>(context);
    easyMobProvider.loadConversations();
  }

  @override
  Widget build(BuildContext context) {
    EasyMobProvider easyMobProvider = Provider.of<EasyMobProvider>(context);
    _allConversations = easyMobProvider.conversations;
    return Scaffold(
      backgroundColor: hex('#fff'),
      appBar: customAppbar(
        title: '通知',
        context: null,
      ),
      body: Container(
        decoration: BoxDecoration(
          border: G.borderTop(),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    ARow(
                      height: 60,
                      leftChild: Container(
                        width: 30,
                        child: Image.asset(
                            "lib/assets/images/message/message_@.png",
                            width: 18,
                            height: 18,
                            fit: BoxFit.contain),
                      ),
                      centerChild: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '小组动态@我',
                          style: TextStyle(color: rgba(56, 56, 56, 1)),
                        ),
                      ),
                      onPressed: () => {G.pushNamed('/group_message')},
                      rightChild: Row(
                        children: <Widget>[
                          icontupian6(color: rgba(197, 196, 196, 1), size: 14)
                        ],
                      ),
                    ),
                    ARow(
                      height: 60,
                      leftChild: Container(
                        width: 30,
                        child: Image.asset(
                            "lib/assets/images/message/message_comment.png",
                            width: 18,
                            height: 18,
                            fit: BoxFit.contain),
                      ),
                      centerChild: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '评论我的',
                          style: TextStyle(color: rgba(56, 56, 56, 1)),
                        ),
                      ),
                      onPressed: () => {G.pushNamed('/comment_message')},
                      rightChild: Row(
                        children: <Widget>[
                          icontupian6(color: rgba(197, 196, 196, 1), size: 14)
                        ],
                      ),
                    ),
                    ARow(
                      height: 60,
                      leftChild: Container(
                        width: 30,
                        child: Image.asset(
                            "lib/assets/images/message/message_apply.png",
                            width: 18,
                            height: 18,
                            fit: BoxFit.contain),
                      ),
                      centerChild: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '小组申请',
                          style: TextStyle(color: rgba(56, 56, 56, 1)),
                        ),
                      ),
                      onPressed: () => {G.pushNamed('/apply_message')},
                      rightChild: Row(
                        children: <Widget>[
                          icontupian6(color: rgba(197, 196, 196, 1), size: 14)
                        ],
                      ),
                    ),
                    ..._allConversations
                        .map((e) => EMConversationListItem(e, this))
                        .toList()
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  void onLongPressConversation(EMConversation conversation, Offset tapPos) {
    // TODO: implement onLongPressConversation
  }

  @override
  void onTapConversation(EMConversation conversation) {
    
    Navigator.push<bool>(context,
        new MaterialPageRoute(builder: (BuildContext context) {
      return ChatPage(conversation: conversation);
    })).then((bool _isRefresh) {
      _loadConversationList();
    });
  }
}
